#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e6 + 10;

int a[N], mx[N], n;
ll tot;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in; rep(i, 1, n - 1) a[i] = in, mx[i] = max(mx[i - 1], a[i]);
	rep(i, 1, n - 1) tot += max(mx[i] - mx[i - 1] - 1, 0);
	if(tot > 1) return puts("0"), 0;
	if(tot == 1) {
		rep(i, 1, n - 1)
			if(mx[i] - mx[i - 1] == 2) {
				rep(j, 0, i) {
					if(mx[i] - mx[j] == 2) return printf("%d\n", i - j), 0;
				}
			}
	} else {
		tot = 1; rep(i, 1, n - 1) tot += mx[i]; cout << tot << endl;
	}
}
